package com.huawei.spare.part.replace.language;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.StopWatch;

import java.io.IOException;
import java.net.*;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.huawei.spare.part.replace.Constants.PingReached;

public class Ping {

    public static void main(String[] args) throws IOException, InterruptedException {
        Locale.setDefault(Locale.ENGLISH);
        StopWatch watch = new StopWatch();
        watch.start();
        Process p1 = java.lang.Runtime.getRuntime().exec("ping -n 5 192.168.0.101");
        int returnVal = p1.waitFor();
        boolean reachable = (returnVal==0);
        System.out.println(reachable);
        final String content = IOUtils.toString(p1.getInputStream(), "GBK");
        System.out.println(content);

        final Matcher matcher = PingReached.matcher(content);
        final boolean matches = matcher.find();
        System.out.println(matches);
        watch.stop();
        System.out.println(watch.toString());
    }

}
